
##
CFLAGS := -g -O0 -Wall
CC := gcc
log := thirdparty/log/log.c thirdparty/log/log.h
json := thirdparty/cJSON/cJSON.c thirdparty/cJSON/cJSON.h
# log_test: test/log_test.c $(log)
# 	-$(CC) $^ -o $@ -I thirdparty
# 	-./$@
# 	-rm $@

#$@ 目标名
#$^ 所有的依赖文件
#-I 给编译器添加头文件搜索路径

# log_test: test/log_test.c $(log)
# 	$(CC) test/log_test.c $(log) -o log_test
# 	./log_test 
# 	rm log_test


# cjson_test: test/cjson_test.c $(json) $(log)
# 	-$(CC) $(CFLAGS) $^ -o $@ -I thirdparty
# 	-./$@
# 	-rm $@


# app_common := app/app_common.c app/app_common.c
# app_common_test : test/app_common_test.c $(app_common) $(log)
# 	-$(CC) $^ -o $@ -I thirdparty -I app
# 	-./$@
# 	-rm $@

# app_message := app/app_message.c app/app_message.h
# app_message_test : test/app_message_test.c $(app_common) $(log) $(app_message) $(json)
# 	-$(CC) $^ -o $@ -I thirdparty -I app
# 	-./$@
# 	-rm $@

# app_mqtt := app/app_mqtt.h app/app_mqtt.c
# app_mqtt_test: test/app_mqtt_test.c $(app_mqtt) $(log)
# 	-$(CC) $^ -o $@ -Ithirdparty -Iapp -lpaho-mqtt3c
# 	-./$@
# 	-rm $@

# mqtt_test: test/mqtt_test.c
# 	-$(CC) $^ -o $@ -lpaho-mqtt3c
# 	-./$@
# 	-rm $@

# app_pool := app/app_pool.h app/app_pool.c
# pool_test: test/app_pool_test.c	$(app_pool) $(log)
# 	-$(CC) $^ -o $@ -Iapp -Ithirdparty
# 	-./$@
# 	-rm $@

# app_buffer := app/app_buffer.h app/app_buffer.c
# buffer_test: test/app_buffer_test.c	$(app_buffer) $(log)
# 	-$(CC) $^ -o $@ -Iapp -Ithirdparty
# 	-./$@
# 	-rm $@

# echo -n -e "\x01\x02\x03\x58\x58\x61\x62\x63" >>test_dev_file

# echo -n -e "\xf9\xf9\x01\x01\x33\x33" >>test_dev_file    
# echo -n -e "\x00\x00\x00\xff\xf1\xdd" >>test_dev_file  
# echo -n -e "\x07\x23\x23\xff\xff\x41" >>test_dev_file
# echo -n -e "\x42\x43\x88\x00\x99\x41" >>test_dev_file

# app_bt := app/app_bt.h app/app_bt.c
# app_device:=app/app_device.c app/app_device.h
# test_device: test/app_device_test.c $(app_device) $(log) $(app_buffer) $(app_message) $(app_common) $(json) $(app_pool) $(app_mqtt) $(app_message) $(app_bt)
# 	-$(CC) $(CFLAGS)  -o $@ $^ -Ithirdparty -Iapp -lpaho-mqtt3c
# 	-./test_device
# 	rm -f test_device

# app_device:=re/app_device_re.c re/app_device_re.h
# test_device: test/app_device_test_re.c $(app_device) $(log) $(app_buffer) $(app_message) $(app_common) $(json) $(app_pool) $(app_mqtt) $(app_message) 
# 	-$(CC) $(CFLAGS)  -o $@ $^ -Ithirdparty -Ire -lpaho-mqtt3c
# 	-./test_device
# 	rm -f test_device



# dev

CC:=$(CROSS_COMPILE)gcc

BOARD_DIR := $(shell pwd)/
PEER := root@192.168.55.78

CFLAGS += -Wall -Wextra

CFLAGS += -I.
CFLAGS += -Ithirdparty
CFLAGS += -Iapp
CFLAGS += -Idaemon
CFLAGS += -Iota

ifdef SYSROOT
	CFLAGS += --sysroot=$(SYSROOT)
endif

LDLIBS += -lpaho-mqtt3c
LDLIBS += -lcurl
LDLIBS += -lcrypto

SRC += $(shell find app -name "*.c"	-type f)
SRC += $(shell find daemon -name "*.c"	-type f)
SRC += $(shell find ota -name "*.c"	-type f)
SRC += $(shell find thirdparty -name "*.c"	-type f)

OBJ := $(SRC:.c=.o)

TARGET := gateway

.PHONY: all, clean

all: $(TARGET)

clean:
	@-rm -f $(TARGET) $(OBJ) main.o

$(TARGET): main.o $(OBJ)
	@-$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)

cross-compile:
	@CROSS_COMPILE=$(BOARD_DIR)/toolchain/bin/arm-linux-gnueabihf- \
	 SYSROOT=$(BOARD_DIR)/sysroot \
	 make -j16
	@scp -O $(TARGET) $(PEER):/usr/bin/$(TARGET)

#cross-init:
#	@scp -O init/S99gateway $(PEER):/etc/init.d/S99gateway

%.o: %.c
	@-$(CC) $(CFLAGS) -c $^ -o $@

%_test: test/%_test.o $(OBJ)
	@-$(CC) $(CFLAGS) $^ -o $@ $(LDLIBS)
	@-./$@
	@-rm $@ $^

# gcc -Ixxx   为gcc指定include目录



ota_http := ota/ota_http.c ota/ota_http.h
ota_http_test : test/ota_http_test.c $(ota_http) $(log) 
	-$(CC) $^ -o $@  -Iota -Ithirdparty -Iapp -lcurl
	-./$@
	rm $@

ota_version := ota/ota_version.c ota/ota_version.h
ota_version_test : test/ota_version_test.c $(ota_http) $(log)  $(ota_version) $(json)
	-$(CC) $^ -o $@  -Iota -Ithirdparty -Iapp -lcurl -lcrypto
	-./$@
	rm $@